package ru.osu.acm.testing.beans;

import ru.osu.acm.common.entities.Contest;
import ru.osu.acm.common.entities.Problem;
import ru.osu.acm.common.entities.Person;
import ru.osu.acm.common.entities.Team;
import ru.osu.acm.common.config.Config;
import ru.osu.acm.common.industrial.ContestFactory;
import ru.osu.acm.common.industrial.ProblemFactory;
import ru.osu.acm.common.industrial.PersonFactory;
import ru.osu.acm.common.industrial.TeamFactory;
import ru.osu.acm.testing.Utils;

import javax.annotation.Resource;
import javax.sql.DataSource;
import javax.faces.model.SelectItem;
import java.util.*;

/**
 * User: Integra
 * Date: 28.08.2009
 * Time: 17:54:18
 */
public class ContestBean {
    @Resource(name = Config.jndi)
    private DataSource source;

    public ArrayList<Contest> getContests() {
        return ContestFactory.getAllContests(Utils.getConnection(source), true);
    }

    private Contest viewingContest = null;

    public Contest getViewingContest() {
        return viewingContest;
    }

    public ArrayList<Problem> getViewingContestProblems() {
        return ProblemFactory.getContestProblems(viewingContest.getId(), Utils.getConnection(source), true);
    }

    public ArrayList<Person> getViewingContestPersons() {
        return PersonFactory.getContestPersons(viewingContest.getId(), Utils.getConnection(source), true);
    }

    public ArrayList<Team> getViewingContestTeams() {
        return TeamFactory.getContestTeams(viewingContest.getId(), Utils.getConnection(source), true);
    }

    private Contest editingContest = null;

    public Contest getEditingContest() {
        return editingContest;
    }

    public ArrayList<Problem> getEditingContestProblems() {
        return ProblemFactory.getContestProblems(editingContest.getId(), Utils.getConnection(source), true);
    }

    public ArrayList<Person> getEditingContestPersons() {
        return PersonFactory.getContestPersons(editingContest.getId(), Utils.getConnection(source), true);
    }

    public ArrayList<Team> getEditingContestTeams() {
        return TeamFactory.getContestTeams(editingContest.getId(), Utils.getConnection(source), true);
    }

    public String removeProblemFromContest() {
        int problemId = Integer.parseInt(Utils.getRequestParam("problem_to_remove"));
        ProblemFactory.removeProblemFromContest(problemId, editingContest.getId(), Utils.getConnection(source), true);
        return null;
    }

    public String addProblemToContest() {
        if (editingContest.getId() == -1) {
            // TODO: Error: save contest first!
        }
        else {
            int problemId = Integer.parseInt(Utils.getRequestParam("contest_editor:problem_to_add"));
            int nextIndex = ContestFactory.getNextProblemIndex(editingContest.getId(), Utils.getConnection(source), true);
            ProblemFactory.addProblemToContest(problemId, editingContest.getId(), nextIndex, Utils.getConnection(source), true);
        }
        return null;
    }

    public String removePersonFromContest() {
        int personId = Integer.parseInt(Utils.getRequestParam("person_to_remove"));
        PersonFactory.removePersonFromContest(personId, editingContest.getId(), Utils.getConnection(source), true);
        return null;
    }

    public String addPersonToContest() {
        if (editingContest.getId() == -1) {
            // TODO: Error: save contest first!
        }
        else {
            int personId = Integer.parseInt(Utils.getRequestParam("contest_editor:person_to_add"));
            PersonFactory.addPersonToContest(personId, editingContest.getId(), Utils.getConnection(source), true);
        }
        return null;
    }

    public String removeTeamFromContest() {
        int teamId = Integer.parseInt(Utils.getRequestParam("team_to_remove"));
        TeamFactory.removeTeamFromContest(teamId, editingContest.getId(), Utils.getConnection(source), true);
        return null;
    }

    public String addTeamToContest() {
        if (editingContest.getId() == -1) {
            // TODO: Error: save contest first!
        }
        else {
            int teamId = Integer.parseInt(Utils.getRequestParam("contest_editor:team_to_add"));
            TeamFactory.addTeamToContest(teamId, editingContest.getId(), Utils.getConnection(source), true);
        }
        return null;
    }

    public ArrayList<SelectItem> getAllProblems() {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        ArrayList<Problem> problems = ProblemFactory.getAllProblems(Utils.getConnection(source), true);
        for (Problem problem : problems) {
            items.add(new SelectItem(problem.getId(), "[" + problem.getId() + "] " + problem.getName()));
        }
        return items;
    }

    public ArrayList<SelectItem> getAllPersons() {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        ArrayList<Person> persons = PersonFactory.getAllPersons(Utils.getConnection(source), true);
        for (Person person : persons) {
            items.add(new SelectItem(person.getId(), "[" + person.getId() + "] " + person.getName() + " " + person.getSurname()));
        }
        return items;
    }

    public ArrayList<SelectItem> getAllTeams() {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        ArrayList<Team> teams = TeamFactory.getAllTeams(Utils.getConnection(source), true);
        for (Team team : teams) {
            items.add(new SelectItem(team.getId(), "[" + team.getId() + "] " + team.getTeamname()));
        }
        return items;
    }

    // Action handlers

    public String viewContest() {
        viewingContest = ContestFactory.getContest(Integer.parseInt(Utils.getRequestParam("contest")), Utils.getConnection(source), true);
        return "contest_viewer";
    }

    public String editContest() {
        editingContest = ContestFactory.getContest(Integer.parseInt(Utils.getRequestParam("contest")), Utils.getConnection(source), true);
        return "contest_editor";
    }

    public String createContest() {
        editingContest = ContestFactory.getNewContest();
        return "contest_editor";
    }

    public String saveContest() {
        ContestFactory.updateContest(editingContest, Utils.getConnection(source), true);
        editingContest = ContestFactory.getContest(editingContest.getId(), Utils.getConnection(source), true);
        return null;
    }
}
